﻿using System;
using System.Collections.Generic;
using System.Text;
using virtuaPol.Util;

namespace virtuaPol.Model
{
    [Serializable]
    [System.Xml.Serialization.XmlType("relation")]
    public class CBaseRelation<T> : INamedObject where T : INamedObject
    {
        // Locker object for thread safety
        private object _locker = new object();

        private string with;
        /// <summary>
        /// The name of the relationship is the name of the object, the relationship
        /// is with.
        /// Example: If country c1 has a relationship with country c2, then this relationship
        /// will be named c2. The owner of the relationship will be c1.
        /// </summary>
        [System.Xml.Serialization.XmlElement("name")]
        public string Name
        {
            get { lock (_locker) { return with; } }
            set { lock (_locker) { with = value; } }
        }

        private string description;
        [System.Xml.Serialization.XmlElement("description")]
        public string Description
        {
            get { lock (_locker) { return description; } }
            set { lock (_locker) { description = value; } }
        }

        private ExtendedList<CProperty> properties = new ExtendedList<CProperty>();
        [System.Xml.Serialization.XmlArray("properties")]
        public ExtendedList<CProperty> Properties
        {
            get { lock (_locker) { return properties; } }
            set { lock (_locker) { properties = value; } }
        }

        public CBaseRelation()
        { }

        public CBaseRelation(T with)
        {
            Name = with.Name;
        }

        public void addProperty(CProperty p)
        {
            Properties.Add(p);
        }

        public CProperty removeProperty(CProperty p)
        {
            Properties.Remove(p);
            return p;
        }

        public CProperty removePropertyByName(string name)
        {
            CProperty p = getPropertyByName(name);
            if (p != null)
                Properties.Remove(p);
            return p;
        }

        public CProperty getPropertyByName(string name)
        {
            if (Properties.Contains(name))
                return Properties[name];
            else
                return null;
        }

        public CProperty getPropertyByIndex(int index)
        {
            return Properties[index];
        }
    }
}
